<?php


/**
 * 虚拟竞标币管理员赠送历史
 * Enter description here ...
 * @author Admin
 *
 */
class  HistorypointbAction extends  CommonAction{
	
	
	/**
	 * 列表首页
	 * Enter description here ...
	 */
	public function index() {
		
		//Log::write("....虚拟竞标币赠送历史查询start..." ,Log::DEBUG);
		
		$Historypoint = D('Transaction');
		
		$username =  $_REQUEST['username'];      //检索条件
		$this->assign("username",$username);
		
		//.....过滤条件:机器人可获赠实体币和虚拟币，注册用户获赠虚拟币
		$map['source'] = '1' ; //赠送类型
		//$map['type'] = 'pointb'; //虚拟竞标币类型
		
		if (!empty($username)){
			$map['name']= array('like',"%{$username}%");  //用户名检索
		}
		
		
		import("@.ORG.Page"); //导入分页类
		$count = $Historypoint->where($map)->count();    //计算总数
		$p = new Page ($count,12);  //分页
		
		$datalist=$Historypoint
			->where($map)
			->limit($p->firstRow.','.$p->listRows)
			->order('addtime desc')    //创建时间降序排列
			->findAll();
			
			
		//...分页检索条件	
		$p->parameter = "username=" . urlencode ( $_REQUEST['username'] ) . "&";

		//...检索条件开始Post，分页后转换Get了，URL将参数传入
		
		
		
		$page = $p->show ();
		
		$this->assign("page",$page);
		$this->assign("datalist",$datalist);
		
		$this->display();
		
		//Log::write("....虚拟竞标币赠送历史查询end..." ,Log::DEBUG);
	}
	
	
	
	
	/**
	 * 赠送虚拟币
	 * Enter description here ...
	 */
	function sendPointb(){
		$this->display();
	}
	
	
	/**
	 * 赠送虚拟币保存
	 * Enter description here ...
	 */
	function sendPointbSave(){
		Log::write("....赠送虚拟币保存start...",Log::DEBUG) ;
		
		$data = Init_GP(array("type","money"));    //接收界面信息 
		
		$pointb = $data['money'];  //赠送虚拟竞标币数
		$type = $data['type'] ;  //赠送类型：全部、注册用户、机器人
		
		//....修改用户虚拟竞标币数，记录虚拟币历史
		$prefix = C('DB_PREFIX');   //数据库前缀
		$user = D('User');
		
		//虚拟竞标币 = 已有虚拟币 + 赠送虚拟币
		
		$sql = "UPDATE {$prefix}user u set u.pointb = u.pointb + {$pointb} " ;
		if($type==0){
			Log::write("....赠送虚拟币给注册用户...",Log::DEBUG) ;
			$sql = $sql . " where u.robot = '0' " ;
			$username = '所有注册用户' ;
		}else if($type==1){
			Log::write("....赠送虚拟币给机器人...",Log::DEBUG) ;
			$sql = $sql . " where u.robot = '1' " ;
			$username = '所有机器人' ;
		}else{
			Log::write("....赠送虚拟币给所有人...",Log::DEBUG) ;
			$username = '所有人' ;
		}
		
		Log::write("....赠送虚拟币SQL.." .$sql ,Log::DEBUG) ;
		
        $st = $user->execute($sql) ;
        
		
		if($st){   //如果保存成功，需记录赠送历史
			
				$config = C('SYSCONFIG');
				$transaction = D('Transaction');   //历史记录表
			
				$transactiondata = array(
					'uid'=>0,    //用户ID
					'name' => $username, //用户姓名
					'payment'=>'管理员',			//支付人
					'money'=>$data['money'],
					'addtime'=>time(),
					'remark'=>"管理员赠送用户{$data['money']}{$config['pointb_name']}",
					'type'=>"pointb",
					'source'=>'1',   //方式是管理员赠送
					'robot'=>$type
				);
				$transaction->add($transactiondata);
			
				
			$this->assign('jumpUrl','__URL__/index');
			$this->success("赠送虚拟币成功");
		}
		
		
		Log::write("....赠送虚拟币保存end...",Log::DEBUG) ;
		
	}
	
	
	
	
	
	/**
	 * 给机器人赠送实体币
	 */
	public function  sendFact(){
		
		$this->display();
	}
	
	
	/**
	 * 保存赠送机器人实体币
	 * Enter description here ...
	 */
	public function sendFactSave(){
		
		$data = Init_GP(array("money"));    //接收界面信息 
		
		$beat = $data['money'];  //赠送虚拟竞标币数
		
		//....修改机器人用户实体竞标币数，记录实体币历史
		$prefix = C('DB_PREFIX');   //数据库前缀
		$user = D('User');
		
		$sql = "UPDATE {$prefix}user u set u.beat = u.beat + {$beat}   where u.robot = '1' " ;    //赠送所有机器人实体币
		
		Log::write("....赠送虚拟币SQL.." .$sql ,Log::DEBUG) ;
		
        $st = $user->execute($sql) ;
        
		
		if($st){   //如果保存成功，需记录赠送历史
			
				$config = C('SYSCONFIG');
				$transaction = D('Transaction');   //历史记录表
			
				$transactiondata = array(
					'uid'=>0,    //用户ID
					'name' => '所有机器人', //用户姓名
					'payment'=>'管理员',			//支付人
					'money'=>$data['money'],
					'addtime'=>time(),
					'remark'=>"管理员赠送用户{$data['money']}{$config['beat_name']}",
					'type'=>"beat",
					'source'=>'1',   //方式是管理员赠送
					'robot'=>'1'
				);
				$transaction->add($transactiondata);
			
				
			$this->assign('jumpUrl','__URL__/index');
			
			$this->success("赠送实体币成功");
		}
		
		
		Log::write("....赠送实体币保存end...",Log::DEBUG) ;
		
		
		
	}
	
	
	
	
	
	
	
	
}




?>